/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Client.domain;

import Client.gui.Drawable;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Ripper
 */
public class ClientGameSession 
{
    private ClientDomainController cdc;
    private String userPlaying;
    
    private List<ClientMob> monsters;
    private List<ClientPlayer> players;
    private List<ClientDecorativeElement> decorativeElements;
    private List<ClientTile> tiles;
    private List<ClientProjectile> projectiles;
    
    private int hitpoints;
    private int maxhitpoints;
    private int manapoints;
    private int maxmanapoints;
    private int staminapoints;
    private int maxstaminapoints;
    
    private ServerMessageProcessor smp;
    
    private double activeplayersx;
    private double activeplayersy;
    
    public ClientGameSession(ClientDomainController cdomcon)
    {
        cdc = cdomcon;
        userPlaying = cdc.getUsername();
        
        monsters = new ArrayList<ClientMob>();
        players = new ArrayList<ClientPlayer>();
        decorativeElements = new ArrayList<ClientDecorativeElement>();
        tiles = new ArrayList<ClientTile>();
        projectiles = new ArrayList<ClientProjectile>();
        
        smp = new ServerMessageProcessor(this);
    }
    
    public void processInformation(String info)
    {
        String[] bitsOfInformation = info.split(";");
        
        for(String s: bitsOfInformation)
        {
            smp.processData(s);
        }
        
        //System.out.println("player list: "+players.size());
    }
    
    public void addTextEffect(Color c, int x, int y, String txt)
    {
        cdc.addTextEffect(c,x,y,txt);
    }
    
    public void setActivePlayersX(double x)
    {
        activeplayersx = x;
    }
    public void setActivePlayersY(double y)
    {
        activeplayersy = y;
    }
    public double getActivePlayersX()
    {
        return activeplayersx;
    }
    public double getActivePlayersY()
    {
        return activeplayersy;
    }

    public ClientDomainController getCdc() {
        return cdc;
    }

    public void setCdc(ClientDomainController cdc) {
        this.cdc = cdc;
    }

    public String getUserPlaying() {
        return userPlaying;
    }

    public void setUserPlaying(String userPlaying) {
        this.userPlaying = userPlaying;
    }

    public List<ClientMob> getMonsters() {
        return monsters;
    }

    public void setMonsters(List<ClientMob> monsters) {
        this.monsters = monsters;
    }

    public List<ClientPlayer> getPlayers() {
        return players;
    }
    public List<ClientTile> getTiles()
    {
        return tiles;
    }
    public List<ClientProjectile> getProjectiles()
    {
        return projectiles;
    }

    public void setPlayers(List<ClientPlayer> players) {
        this.players = players;
    }
    
    public List<ClientDecorativeElement> getDecorativeElements()
    {
        return decorativeElements;
    }
    public void addProjectile(ClientProjectile p)
    {
        projectiles.add(p);
    }
    public void addPlayer(ClientPlayer p)
    {
        players.add(p);
        //System.out.println("player added. now there are "+players.size()+" players.");
    }
    public void addMonster(ClientMob m)
    {
        monsters.add(m);
    }
    public void addDecorativeElement(ClientDecorativeElement cde)
    {
        decorativeElements.add(cde);
    }
    public void removeProjectile(int id)
    {
        ClientProjectile cp = null;
        for(ClientProjectile p: projectiles)
        {
            if(p.getId() == id)
            {
                cp = p;
            }
        }
        
        if(cp != null)
        {
            projectiles.remove(cp);
        }
    }
    public void removePlayer(ClientPlayer p)
    {
        players.remove(p);
        System.out.println("player remove. now there are "+players.size()+" players.");
    }
    public void removePlayer(String plrnm)
    {
        ClientPlayer cp = null;
        for(ClientPlayer cpu: players)
        {
            if(cpu.getPlayerName().toLowerCase().equals(plrnm.toLowerCase()))
            {
                cp = cpu;
            }
        }
        
        if(cp != null)
        {
            removePlayer(cp);
        }
    }
    public void removeMonster(ClientMob m)
    {
        monsters.remove(m);
    }
    public void removeMonster(int id)
    {
        ClientMob cm = null;
        for(ClientMob cmos: monsters)
        {
            if(cmos.getId() == id)
            {
                cm = cmos;
            }
        }
        
        if(cm != null)
        {
            removeMonster(cm);
        }
    }
    public void removeDecorativeElement(int id)
    {
        ClientDecorativeElement cde = null;
        for(ClientDecorativeElement cdee: decorativeElements)
        {
            if(cdee.getId() == id)
            {
                cde = cdee;
            }
        }
        
        if(cde != null)
        {
            decorativeElements.remove(cde);
        }
    }

    public int getHitpoints() {
        return hitpoints;
    }

    public int getManapoints() {
        return manapoints;
    }

    public int getMaxhitpoints() {
        return maxhitpoints;
    }

    public int getMaxmanapoints() {
        return maxmanapoints;
    }

    public int getMaxstaminapoints() {
        return maxstaminapoints;
    }

    public int getStaminapoints() {
        return staminapoints;
    }
    
    public int getHPprct()
    {
        try
        {
            return (getHitpoints()*100)/getMaxhitpoints();
        }
        catch(Exception ex)
        {
            return 0;
        }
    }
    
    public int getSPprct()
    {
        try
        {
            return (getStaminapoints()*100)/getMaxstaminapoints();
        }
        catch(Exception ex)
        {
            return 0;
        }
    }
    
    public int getMPprct()
    {
        try
        {
            return (getManapoints()*100)/getMaxmanapoints();
        }
        catch(Exception ex)
        {
            return 0;
        }
    }
    
    public ClientPlayer getPlayer(String plrname)
    {
        for(ClientPlayer cp: players)
        {
            if(cp.getPlayerName().toLowerCase().equals(plrname.toLowerCase()))
            {
                return cp;
            }
        }
        return null;
    }
    public ClientMob getMonster(int m_id)
    {
        for(ClientMob cm: monsters)
        {
            if(cm.getId() == m_id)
            {
                return cm;
            }
        }
        return null;
    }

    public void setHitpoints(int hitpoints) {
        this.hitpoints = hitpoints;
    }

    public void setManapoints(int manapoints) {
        this.manapoints = manapoints;
    }

    public void setMaxhitpoints(int maxhitpoints) {
        this.maxhitpoints = maxhitpoints;
    }

    public void setMaxmanapoints(int maxmanapoints) {
        this.maxmanapoints = maxmanapoints;
    }

    public void setMaxstaminapoints(int maxstaminapoints) {
        this.maxstaminapoints = maxstaminapoints;
    }

    public void setSmp(ServerMessageProcessor smp) {
        this.smp = smp;
    }

    public void setStaminapoints(int staminapoints) {
        this.staminapoints = staminapoints;
    }
    
    public List<Drawable> getDrawablePlayers()
    {
        List<Drawable> drawables = new ArrayList<Drawable>();
        for(ClientPlayer cp: players)
        {
            drawables.add(cp);
        }
        
        return drawables;
    }
    public List<Drawable> getDrawableMonsters()
    {
        List<Drawable> drawables = new ArrayList<Drawable>();
        for(ClientMob cm: monsters)
        {
            drawables.add(cm);
        }
        
        return drawables;
    }
    
    public void playerHasSpoken(String plr, String encodedMsg)
    {
        cdc.playerHasSpoken(plr,encodedMsg);
    }

    public void addTile(ClientTile ct) 
    {
        tiles.add(ct);
    }
}
